// pages/address/add.js

import http from '../../utils/http.js';
import api from '../../utils/api.js';
import message from '../../utils/message';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{
      username:'',
      mobile:'',
      address:'',
      address_name:'',
      is_default:0,
      user_id:1,
      province:'',
      city:'',
      area:'',
     
    },
    customItem:'全部',
    provinces:[],
    province_index:-1,
    city_list:[],
    city_index:-1,

    area_list:[],
    area_index:-1,
    set_location:false,
    has_location:false,

    username:'',
    phone:'',
    detail:'',
    district:'',
    select_province:null,
    select_city:null,
    select_area:null,
    select_status:0,
    is_first:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;

    var type="add";
    if(options.type){

      
      type= options.type;
      if(type=='edit'){
        this.setData({
          type: 'edit'
        })

        wx.setNavigationBarTitle({
          title: '编辑收货地址',
        })

        this.getAddressDetail( options.id);

      }


    }

    


    wx.showShareMenu({
      withShareTicket:true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success:function( res ){
        console.log(res);
      },
      fail:function(){

      },
      complete:function(){

      }
    })

    api.getProvinceList({} , function( response ){
      me.setData({
        provinces: response.data
      })
    })

    var me = this;
    setTimeout(function(){
      me.setData({
        is_first:false
      })
    } , 1000)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    /*
    var object = wx.getLaunchOptionsSync();

    var t = JSON.stringify(object);

    wx.showModal({
      content: t,
      title:'提示'
    })

    */

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    var _this = this;


    /*
    var promise = new Promise( resolve=>{
      setTimeout(()=>{
        resolve({
          title:'自定义转发标题'
        })
      } , 200)
    })

    */

    return {
      title:'测试转发到朋友全',
      path:'/pages/address/add',
      imageUrl:'/images/cart_add.png',
      success:function( result ){
        console.log(result);
      },
      fail:function(){
        console.log("share error");
      },
      complete:function(){
        console.log("pppp");
      }
    }

  },

  inputBlur: function (e) {
    //console.log(JSON.stringify(e));
    var name = e.currentTarget.dataset.name;
    var value = e.detail.value;
    //var data = '{"form":{"' + name + '":"' + value + '"}}';
    var data = '{"' + name + '":"' + value + '"}';
    var address = this.data.address;
    address[name] = value;
    this.setData({
      address:address
    })
    //this.setData(JSON.parse(data));
  },
  saveAddress: function(e){
    var address = this.data.address;
    var me = this;

    /*
    var data = {
      "username":me.data.username,
      "mobile":me.data.phone,
      "district":me.data.district,
      "address_detail":me.data.address_detail
    }
    */
    console.log(address);

    var result =   this.validateData(address);

    if(!result){
      return ;
    }
    
    message.show_loading();
    api.addAddress( address , resp=>{
      message.hide_loading();
      if(resp.status==1){
        wx.showModal({
          title: '提示',
          content: resp.message,
          success: function (res) {

            if (res.confirm) {
              wx.navigateBack();
            }

          }
        })
      }else{
        wx.showToast({
          title: resp.message,
          icon: 'error'
        })
      }

    })


    

  },

  validateData:function( address ){
    /**
     * 验证收货地址数据是否正确
     */

    /*
    "username":me.data.username,
      "phone":me.data.phone,
      "district":me.data.district,
      "address_detail":me.data.address_detail
      */
    if( address.username=='' || address.username.length<=0 ){
      message.error("收件人姓名不能为空");
      return false;
    }

    if(address.mobile.length<=0){
      message.error("手机号不能为空");
      return false;
    }

    

    var reg_phone  = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
    if( !reg_phone.test( address.mobile ) ){
      message.error("手机号格式异常，请填写正确的手机号");
      return false;
    }

    if(address.province=='' || address.province.length<=0){
      message.error("请选择省份")
      return false;
    }

    if(address.city==''|| address.city.length<=0){
      message.error("请选择城市");
      return false
    }

    if(address.area==''|| address.area.length<=0){
      message.error("请选择区、县");
      return false
    }

    if(address.address == '' || address.address.length<=0){
      message.error("请填写详细地址");
      return false;
    }

    return true;

  },
  updatePostName:function( e ){

  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  cityChange:function( e ){

    var value = e.detail.value;

    var provinces = this.data.provinces;
    console.log(value)

    var province_no = value[0]
    var city_no = value[1]
    var area_no = value[2]

    if(province_no!=this.data.province_no){
      var id = provinces[province_no].id;

    }

  },
  changeRegionLevel:function( e){ 
    var index = e.currentTarget.dataset.index;
    var province = this.data.provinces[index];
    console.log( province);

    var params ={};
    params.province_code = province.province_code;
    var me = this;
    api.getCityList( params  , function( result ){

      me.setData({
        city_index:-1,
        city_list:result.data,
        select_status:1
        })
      })
    this.setData({
      province_index:index,
      select_province: province,
      select_city:null,
      select_area:null
    })

  },
  changeCityRegion:function(e){
    var me = this;
    var index = e.currentTarget.dataset.index;
    var province = this.data.provinces[index];
    console.log( province);
    if(index<0){
      return ;
    }

    var city = this.data.city_list[index];

    var params = {}
    params.city_code = city.city_code;

    this.setData({
      city_index:index
    })

    api.getAreaList( params , function( result ){
      me.setData({
        area_list:result.data,
        area_index:-1,
        select_city: city,
        select_area:null,
        select_status:2
      })
    })
    
  },
  changeAreaRegion:function( e ){
    var index = e.currentTarget.dataset.index;

    var area = this.data.area_list[index];
    this.setData({
      area_index:index,
      select_area: area
    })

  },
  confirmLocation(e){
    var index = e.currentTarget.dataset.index;
    if(index==1){
      //打开地址选择界面

      this.setData({
        set_location:true
      })
    }

    if(index==0){
      //取消地址设置
      this.setData({
        set_location:false
      })
    }

    if(index==2){
      //保存地址设置
      var has_location = true

      var province_index = this.data.province_index;
      if(province_index==-1 ){
        has_location= false;
      }

      var city_index = this.data.city_index;
      if(city_index==-1){
        has_location= false;
      }

      var area_index = this.data.area_index;
      if(this.data.area_index==-1){
        has_location= false;

      }

      if( has_location==false){

        var address = this.data.address;
        console.log(address);
        address.province = "";
        address.city = "";
        address.area = "";

        this.setData({
          address:address,
          set_location:false,
          has_location:false
        })

      }

      var province = this.data.provinces[province_index];
      var city = this.data.city_list[city_index ];
      var area = this.data.area_list[area_index];

      var address = this.data.address;
      console.log(address);
      address.province = province.province_name;
      address.city = city.city_name;
      address.area = area.area_name;

      this.setData({
        address:address,
        set_location:false,
        has_location:true
      })

      
    }
  },
  getAddressDetail:function( address_id ){
    var me = this;
    api.getAddressDetail( { id:address_id} , function( response ){

      console.log(response);

      if(response.status==1){
        me.setData({
          address: response.data,
          has_location: true
        })
      }else{

      }



    } )
  },
  selectStatus:function( e ){
    var status = e.currentTarget.dataset.index;
    this.setData({
      select_status: status
    })
  }
    
})